package tool;


import java.awt.Component;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

public class Tools {
	
	private static Logger _log = Logger.getLogger(Tools.class.getName());
	
	
	/**移除TABLE裡面所有資料
	 * @param DTM
	 */
	public static void removeTableItems(DefaultTableModel DTM){
		int size = DTM.getRowCount();

    	//Table clear
    	try{
            for(int j=0;j<size;j++){
            	DTM.removeRow(0);
            }
        }catch(ArrayIndexOutOfBoundsException e){
        	_log.log(Level.WARNING, e.getMessage(),e);
        }
	}
	
	/**Calendar轉成yyyy-MM-dd
	 * @param cale
	 * @return
	 */
	public static String dateFormatDay(Calendar cale){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(cale.getTime());
	}
	
	/**Calendar轉成yyyy-MM-dd HH:mm:ss
	 * @param cale
	 * @return
	 */
	public static String dateFormatSecond(Calendar cale){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(cale.getTime());
	}
	
	/**由字串時間轉成Calendar
	 * @param time
	 * @return
	 */
	public static Calendar getDateSecond(String time){
		Calendar cale = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			cale.setTime(sdf.parse(time));
		} catch (ParseException e) {
			_log.log(Level.WARNING, e.getMessage(),e);
			_log.log(Level.WARNING, e.getMessage(),e);
		}
		return cale;
	}
	
	/**由字串時間轉成Calendar
	 * @param time
	 * @return
	 */
	public static Calendar getDate(String time){
		Calendar cale = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			cale.setTime(sdf.parse(time));
		} catch (ParseException e) {
			_log.log(Level.WARNING, e.getMessage(),e);
		}
		return cale;
	}
	
	/**視窗放中間
	 * @param com
	 */
	public static void setComponentCenter(Component com){
		Dimension dime = Toolkit.getDefaultToolkit().getScreenSize();
		com.setLocation((int)(dime.getWidth()-com.getWidth())/2
				, (int)(dime.getHeight()-com.getHeight())/2);
	}
	
	/**取得SQL時間轉成Calendar
	 * @param rs
	 * @param name
	 * @return
	 * @throws SQLException
	 */
	public static Calendar getSQLDate(ResultSet rs, String name) throws SQLException{
		Calendar cale = Calendar.getInstance();
		
		cale.setTimeInMillis(rs.getDate(name).getTime()+rs.getTime(name).getTime());
		//要+8小時  台灣時間
		cale.add(Calendar.HOUR, 8);
		return cale;
	}
	
	/**取得現在時間 yyyy-MM-dd
	 * @return
	 */
	public static String getNowTimeDay(){
		return dateFormatDay(Calendar.getInstance());
	}
	
	/**取得現在時間 yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static String getNowTimeSecond(){
		return dateFormatSecond(Calendar.getInstance());
	}
	
    public static void showError(String ErrStr){
    	JOptionPane.showMessageDialog(null,
                ErrStr,
                "錯誤訊息", JOptionPane.PLAIN_MESSAGE);
    }
    //if(information.showSure("你確定要取消嗎?") == PSOptionPanel.YES_OPTION){
    public static int showSure(String sureStr){
        return JOptionPane.showOptionDialog(
                null,
                sureStr,
                "確認訊息", 
                JOptionPane.OK_CANCEL_OPTION,
                JOptionPane.PLAIN_MESSAGE,
                null,
                null,null);
        
    }
    
    public static void showMSG(String Title, String Context){
    	JOptionPane.showMessageDialog(
                    null,
                    Context,
                    Title,
                    JOptionPane.PLAIN_MESSAGE);
    }
    
    public static void showMSG(String Context){
    	JOptionPane.showMessageDialog(
                null,
                Context,
                "訊息",
                JOptionPane.PLAIN_MESSAGE);
    }
}
